package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.DownloadImgRequest;
import com.frogs.ucenter.v1.request.ImagesDeleteRequest;
import com.frogs.ucenter.v1.request.ImagesListRequest;
import com.frogs.ucenter.v1.request.UploadImgRequest;
import com.frogs.ucenter.v1.service.UploadService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION+"UploadImgController")
@RequestMapping("/"+ApiConstants.VERSION)
public class UploadImgController {
	Logger log = Logger.getLogger(UploadImgController.class);
	
	@Resource(name=ApiConstants.VERSION+"uploadService")
	private UploadService uploadService;
	
	private static final String IMG_UPLOAD = "/images/upload"; 
	private static final String IMG_DETAIL = "/images/detail";
	private static final String IMG_LIST = "/images/list"; 
	private static final String IMG_DELETE = "/images/delete"; 
	private static final String IMG_DOWNLOAD = "/images/download";
	
	/**
	 * 上传图片
	 * @param request
	 * @return
	 */
	@RequestMapping(IMG_UPLOAD)
	public Response imgUpload(HttpServletRequest request){
		Response response = null;
		try {
			UploadImgRequest uploadRequest = new UploadImgRequest();
			uploadRequest.parse(request);
			response = uploadService.doUploadImg(request,uploadRequest);
		} catch(Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		
		return response;
	}
	

	/**
	 * 下载图片
	 * @param request
	 * @return
	 */
	@RequestMapping(IMG_DOWNLOAD)
	public Response imgDownload(HttpServletRequest request){
		Response response = null;
		try {
			DownloadImgRequest downloadRequest = new DownloadImgRequest(); 
			downloadRequest.parse(request);
			response = uploadService.doDownloadImg(downloadRequest);
		} catch(Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 获取单张图片
	 * @param request
	 * @return
	 */
	@RequestMapping({IMG_DETAIL})
	public Response imgDetail(HttpServletRequest request){
		Response response = null;
		try {
				UploadImgRequest uploadRequest = new UploadImgRequest();
				uploadRequest.parse(request);
				response = uploadService.getUploadedImg(request, uploadRequest);
		} catch(Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 获取文件列表
	 * @param request
	 * @return
	 */
	@RequestMapping(IMG_LIST)
	public Response imgList(HttpServletRequest request){
		Response response = null;
		try {
			ImagesListRequest imagesListRequest = new ImagesListRequest();
			imagesListRequest.parse(request);
			response = uploadService.listUploadedImg(request, imagesListRequest);
		} catch(Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 删除文件
	 * @param request
	 * @return
	 */
	@RequestMapping(IMG_DELETE)
	public Response imgDelete(HttpServletRequest request){
		Response response = null;
		try {
			ImagesDeleteRequest imagesDeleteRequest = new ImagesDeleteRequest();
			imagesDeleteRequest.parse(request);
			response = uploadService.deleteUploadedImg(request, imagesDeleteRequest);
		} catch(Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
}
